const jwt = require("jsonwebtoken");
const { dbName, http } = require("../modal/db.config");
const SECRET = dbName;
const { getCurrentDateTime } = require("../modal/time.config");
exports.login = async (req, res) => {
  const body = req.body;
  const { phone, password } = body;
  // 假设已经验证了用户凭据，现在要签发JWT
  const user = await req.services.UserService.find({ phone, password });
  if (user && user.id) {
    // 创建JWT
    const token = jwt.sign({ userId: user.id, role: "user" }, SECRET, {
      expiresIn: "24h",
    }); // 设置过期时间
    res.status(200).json({ data: token, msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "账号/密码错误", msg: "fail", code: 400 });
  }
};
exports.adminLogin = async (req, res) => {
  const body = req.body;
  const { username, password } = body;
  // 假设已经验证了用户凭据，现在要签发JWT
  if (username === "admin" && password === "123456") {
    // 创建JWT
    const token = jwt.sign({ userId: 0, role: "admin" }, SECRET, {
      expiresIn: "24h",
    }); // 设置过期时间
    res.status(200).json({ data: token, msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "账号/密码错误", msg: "fail", code: 400 });
  }
};
exports.register = async (req, res) => {
  const body = req.body;
  const { phone, username, password ,sex} = body;
  const user = await req.services.UserService.findByPhone(phone);
  if (user && user.id) {
    res.status(200).json({ data: "手机号已被注册", msg: "fail", code: 400 });
    return;
  }
  const avatar = http + "/upload/avatar_nomal.png";
  const data = {
    avatar,
    phone,
    username,
    password,
    sex,
    create_time: getCurrentDateTime(),
    update_time: getCurrentDateTime(),
  };
  const newUser = await req.services.UserService.createUser(data);
  if (newUser.affectedRows === 1) {
    res.status(200).json({ data: "新增成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};
exports.update = async (req, res) => {
  const body = req.body;
  const { phone, username, password, avatar ,sex} = body;
  const update_time = getCurrentDateTime();
  const user = await req.services.UserService.decodeToken();
  const userId = user.userId;
  console.log();
  const data = {
    avatar,
    phone,
    username,
    password,
    update_time,
    sex
  };
  const newUser = await req.services.UserService.update(data, { id: userId });
  if (newUser.affectedRows === 1) {
    res.status(200).json({ data: "更新成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.list = async (req, res) => {
  const body = req.body;
  const { username = "", page = null, pageSize = null } = body;
  let newData = await req.services.UserService.getList({
    pageSize: pageSize,
    page: page,
    like: { username },
  });
  let count = await req.services.UserService.getCount({
    like: { username },
  });

  res.status(200).json({
    data: {
      list: newData,
      total: count,
    },
    msg: "success",
    code: 200,
  });
};

exports.getDetail = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const user = await req.services.UserService.decodeToken();
  let userId = user.role === "user" ? user.userId : id;
  const newUser = await req.services.UserService.findById(userId);
  if (newUser) {
    res.status(200).json({ data: newUser, msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无此人", msg: "fail", code: 400 });
  }
};

exports.del = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newUser = await req.services.UserService.delete(Number(id));
  if (newUser.affectedRows === 1) {
    res.status(200).json({ data: "删除成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无此人", msg: "fail", code: 400 });
  }
};
